﻿@page "/tests/markdown"
<Row>
    <Column>
        <Card Margin="Margin.Is4.OnY">
            <CardHeader>
                <CardTitle>Markdown editor</CardTitle>
            </CardHeader>
            <CardBody>
                <Fields>
                    <Field ColumnSize="ColumnSize.IsAuto">
                        <Button Color="Color.Primary" Clicked="@(()=>markdownRef.SetValueAsync("this is a new value from SetValueAsync Method"))">Set SetValueAsync Method</Button>
                    </Field>
                    <Field ColumnSize="ColumnSize.IsAuto">
                        <Button Color="Color.Primary" Clicked="@(()=>markdownValue = "this is a new value from Value Parameter")">Set Value Parameter</Button>
                    </Field>
                </Fields>
            </CardBody>
            <CardBody>
                <Row>
                    <Column>
                        <Markdown @ref="@markdownRef"
                                  Value="@markdownValue" ValueChanged="@OnMarkdownValueChanged"
                                  ImageUploadChanged="@OnImageUploadChanged"
                                  ImageUploadStarted="@OnImageUploadStarted"
                                  ImageUploadProgressed="@OnImageUploadProgressed"
                                  ImageUploadEnded="@OnImageUploadEnded" />
                    </Column>
                    <Column>
                        @((MarkupString)markdownHtml)
                    </Column>
                </Row>
            </CardBody>
        </Card>
    </Column>
</Row>
@code {
    Markdown markdownRef;

    string markdownValue = "# EasyMDE \n Go ahead, play around with the editor! Be sure to check out **bold**, *italic*, [links](https://google.com) and all the other features. You can type the Markdown syntax, use the toolbar, or use shortcuts like `ctrl-b` or `cmd-b`.";

    string markdownHtml;

    protected override void OnInitialized()
    {
        markdownHtml = Markdig.Markdown.ToHtml( markdownValue ?? string.Empty );

        base.OnInitialized();
    }

    Task OnMarkdownValueChanged( string value )
    {
        markdownValue = value;

        markdownHtml = Markdig.Markdown.ToHtml( markdownValue ?? string.Empty );

        return Task.CompletedTask;
    }

    //string fileContent;
    int fileProgress;

    async Task OnImageUploadChanged( FileChangedEventArgs e )
    {
        try
        {
            foreach ( var file in e.Files )
            {
                using ( var stream = new System.IO.MemoryStream() )
                {
                    await file.WriteToStreamAsync( stream );

                    //stream.Seek( 0, SeekOrigin.Begin );

                    //using ( var reader = new StreamReader( stream ) )
                    //{
                    //    fileContent = await reader.ReadToEndAsync();
                    //}
                }
            }
        }
        catch ( Exception exc )
        {
            Console.WriteLine( exc.Message );
        }
        finally
        {
            this.StateHasChanged();
        }
    }

    Task OnImageUploadStarted( FileStartedEventArgs e )
    {
        fileProgress = 0;
        Console.WriteLine( $"Started Image: {e.File.Name}" );

        return Task.CompletedTask;
    }

    Task OnImageUploadEnded( FileEndedEventArgs e )
    {
        if ( e.Success )
            // since we're faking the upload in this demo we will just insert some dummy imageUrl
            e.File.UploadUrl = "https://images.pexels.com/photos/4966601/pexels-photo-4966601.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=200";
        else
            e.File.UploadUrl = string.Empty;

        Console.WriteLine( $"Finished Image: {e.File.Name}, {nameof( e.Success )}: {e.Success}, {nameof( e.FileInvalidReason )}: {e.FileInvalidReason}" );

        return Task.CompletedTask;
    }

    Task OnImageUploadProgressed( FileProgressedEventArgs e )
    {
        fileProgress = (int)e.Percentage;
        Console.WriteLine( $"Image: {e.File.Name} Progress: {e.Percentage}" );

        return Task.CompletedTask;
    }

    Task OnImageUploadWritten( FileWrittenEventArgs e )
    {
        Console.WriteLine( $"Image: {e.File.Name} Position: {e.Position} Data: {Convert.ToBase64String( e.Data )}" );

        return Task.CompletedTask;
    }
}